
let n = 5
let m = 5
let migong = [['.','.','.','.','.'],['.','R','R','D','.'],['.','U','.','D','R'],['.','U','L','L','.'],['.','.','.','.','O']]
let grids = new Array(n).fill().map(()=>new Array(m).fill(-1))
let start_x = 0
let start_y = 0
let count = 0
let visits = new Array(n).fill().map(()=>new Array(m).fill(-1))
// 遍历一边找到出口位置
for(let i=0;i<n;i++){
	for(let j=0;j<m;j++){
		if(migong[i][j]==='O'){
			grids[i][j] = 0
			start_x = i
			start_y = j
		}
	}
}
console.log(grids)
visits[start_x][start_y] = 0
let queue = []
queue.push([start_x,start_y])
while(queue.length!==0){
	let [x,y] = queue.shift()
	// 上
	if(x-1>=0 && visits[x-1][y]===-1 && (migong[x-1][y]==='.'||migong[x-1][y]==='D')){
		grids[x-1][y] = 0
		visits[x-1][y] = 0
		queue.push([x-1,y])
	}
	// 下
	if(x+1<m && visits[x+1][y]===-1 && (migong[x+1][y]==='.'||migong[x+1][y]==='U')){
		grids[x+1][y] = 0
		visits[x+1][y] = 0
		queue.push([x+1,y])
	}
	// 左
	if(y-1>=0 && visits[x][y-1]===-1 && (migong[x][y-1]==='.'||migong[x][y-1]==='R')){
		grids[x][y-1] = 0
		visits[x][y-1] = 0
		queue.push([x,y-1])
	}
	// 右
	if(y+1<n && visits[x][y+1]===-1 && (migong[x][y+1]==='.'||migong[x][y+1]==='L')){
		grids[x][y+1] = 0
		visits[x][y+1] = 0
		queue.push([x,y+1])
	}
}
// 遍历一边统计 -1的个数 就是不能到达的位置个数
for(let i=0;i<n;i++){
	for(let j=0;j<m;j++){
		if(grids[i][j]===-1){
			count ++
		}
	}
}

console.log(grids)
console.log(count)